<?php

class ListUserLockForm extends Form
{
	function ListUserLockForm(){
		Form::Form('ListUserLockForm');				
		$this->link_css('style/manage_item.css');
		$this->link_css('style/aznet/ui.datepicker.css');		
		$this->link_js('javascript/jquery/ui.datepicker.js');					
	}
	
	function on_submit(){		
		
	}	
	
	function draw(){
		global $display;
		$this->beginForm(false,"POST",false,Url::build_current());		
		
		$search_value = " 1 ";
		
		$type_check = Url::get('type_check',1);	
		$display->add('type_check',$type_check);
		
		if($type_check==2){
			$search_value = ' type=2';
		}
		else{
			$search_value = ' type>=0';
		}
		
		$order_by = " ORDER BY time DESC";					
				
		//search theo ngay thang nam
		$display->add('created_time_from',Url::get('created_time_from'));
		$display->add('created_time_to',Url::get('created_time_to'));
		
		$created_time_from=0;
		$created_time_to=0;
		if(Url::get('created_time_from')){
			$date_arr = explode('-',Url::get('created_time_from'));
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		if(Url::get('created_time_to')){
			$date_arr = explode('-',Url::get('created_time_to'));
			
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}		
		
		if($created_time_from){																	
			$search_value .= ' AND time >= '.$created_time_from;
		}
		if($created_time_to){																	
			$search_value .= ' AND time <= '.$created_time_to;
		}
		
		///// Tim ADMIN 
		
		if(Url::get('admin_text_value')!=''){		
			$admin_text_value=Url::get('admin_text_value');
			$display->add('admin_text_value',$admin_text_value);
			
			$str_search = str_replace( "'" , '"', $admin_text_value );
			$str_search = str_replace( "&#39;" , '"', $str_search );
			$str_search = str_replace( "&quot;" , '"', $str_search );
								
			$search_value .= " AND admin_name LIKE '%".$str_search."%'";
		}
		
		
		if(Url::get('admin_text_value_open')!=''){		
			$admin_text_value_open=Url::get('admin_text_value_open');
			$display->add('admin_text_value_open',$admin_text_value_open);
			
			$str_search = str_replace( "'" , '"', $admin_text_value_open );
			$str_search = str_replace( "&#39;" , '"', $str_search );
			$str_search = str_replace( "&quot;" , '"', $str_search );
								
			$search_value .= " AND unlock_user LIKE '%".$str_search."%'";
		}
				
		$admin_id_search=(int)Url::get('admin_id_search',0);		
		if($admin_id_search){
			$search_value .= ' AND admin_id='.$admin_id_search;
		}
		if($admin_id_search==0){
			$admin_id_search='';
		}		
		$display->add('admin_id_search',$admin_id_search);
		
		
		///// Tim User 
		// search ô textbox	tài khoản
		if(Url::get('text_value')!=''){		
			$text_value=Url::get('text_value');
			$display->add('text_value',$text_value);
			
			$str_search = str_replace( "'" , '"', $text_value );
			$str_search = str_replace( "&#39;" , '"', $str_search );
			$str_search = str_replace( "&quot;" , '"', $str_search );
								
			$search_value .= " AND user_lock.user_name LIKE '%".$str_search."%'";
		}
		
		// search ô textbox	 ID
		$id_search=(int)Url::get('id_search',0);		
		if($id_search){
			$search_value .= ' AND user_lock.user_id='.$id_search;
		}
		if($id_search==0){
			$id_search='';
		}		
		$display->add('id_search',$id_search);				
						
		$paging = '';
		$item_per_page = Url::get('item_per_page',50);
		$sql_count='SELECT COUNT(id) AS total_item FROM user_lock WHERE '.$search_value;	
		$total=DB::fetch($sql_count,'total_item',0);		
		$items=array();		
		if($total){
			$limit='';
			
			$paging = AZPagging::pagingSE($limit,$total,$item_per_page,10,'page_no',true,'Thành viên','Trang');			
			$sql = 'SELECT user_lock.*, user.block_time, user.invalid_time FROM user_lock INNER JOIN user ON user.id=user_lock.user_id WHERE '.$search_value.$order_by.$limit;			
			$result = DB::query($sql);			
									
			if($result){
				while ($row=mysql_fetch_assoc($result)){
					$row['time'] = date('H:i d/m/y',$row['time']);
					$row['is_block'] = false;
					if($row['unlock_time']>0){
						$row['unlock_time'] = date('H:i d/m/y',$row['unlock_time']);
					}
					else{
						$row['unlock_time'] = "";
					}
					$row['unban_nick'] = Url::build_all(array('chk_id','del_all','cmd','id','lock_die_all','hd_ac'),'cmd=unban_nick&id='.$row['id'].'&user_id='.$row['user_id']);
					
					if($row['block_time']>TIME_NOW){
						$row['is_block'] = "khóa theo ngày";
					}
					elseif($row['block_time']==-1){
						$row['is_block'] = "khóa vĩnh viễn";
					}
					
					//$row['is_block'] .= $row['is_block']!=""?" & ":"";
					
					if($row['invalid_time']>TIME_NOW){
						$row['is_block'] = "KD theo ngày";
					}
					elseif($row['invalid_time']==-1){
						$row['is_block'] = "KD vĩnh viễn";
					}
										
					$items[$row['id']]=$row;
				}				
			}
		}				
				
		$display->add('total_user',$total);		
		$display->add('items',$items);
		$display->add('paging',$paging);		
		$display->output('ManageUserLock');		
		$this->endForm();
		
	}	
}
?>
